<ui:composition xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:fn="http://java.sun.com/jsp/jstl/functions"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets" template="/home.xhtml">
	<ui:define name="corpo">
		<h:form prependId="false">
			<p:panel header="Administrador" style="background: #dbfbda;font-size:15px;min-height:700px;margin-top:10px;float:left;width:99%">
				<h:panelGroup style="float: left;width:250px;" layout="block">
					    <ui:include src="menu.xhtml" />
				</h:panelGroup>
				
				<h:panelGroup layout="block" style="float:left">
					<p:panel header="Consultas" style="width:700px;" id="pnl_edicao">
					
						<h:panelGrid columns="4" cellspacing="5" cellpadding="4" style="margin-left:100px">
							<h:outputLabel value="Área médica:" />
							<h:selectOneMenu value="#{manterAdministradorBean.administradorForm.areaMedica}" id="areaMedica">
								<f:selectItem itemLabel="" noSelectionOption="true"/>
								<f:selectItems value="#{manterAdministradorBean.administradorForm.areasMedicas}" var="entity" itemLabel="#{entity.areaMedica}" itemValue="#{entity}" />
								<p:ajax event="change" listener="#{manterAdministradorBean.administradorForm.obterMedicosPorAreaMedica}" process="@this" update="medicos" />
							</h:selectOneMenu>
							
							<h:outputLabel value="Médico:" />
							<h:selectOneMenu id="medicos" value="#{manterAdministradorBean.administradorForm.consulta.medico}" style="width:150px">
								<f:selectItem itemLabel="" noSelectionOption="true"/>
								<f:selectItems value="#{manterAdministradorBean.administradorForm.listMedico}" var="entity" itemValue="#{entity}" itemLabel="#{entity.nome}" />
								<f:converter converterId="convertEntity"/>
							</h:selectOneMenu>
							
							<h:outputLabel value="Data início:" />
							<p:inputMask mask="99/99/9999" value="#{manterAdministradorBean.administradorForm.dataInicio}">
								<f:convertDateTime locale="pt_BR" timeZone="GMT-3" />
							</p:inputMask>
							
							<h:outputLabel value="Data fim:" />
							<p:inputMask mask="99/99/9999" value="#{manterAdministradorBean.administradorForm.dataFim}">
								<f:convertDateTime locale="pt_BR" timeZone="GMT-3" />
							</p:inputMask>
														
						</h:panelGrid>
						
						<h:panelGroup layout="block" style="text-align:center">
							<p:commandButton value="Listar Consultas" action="#{manterAdministradorBean.listarConsultas}" update="table_consultas" />	
						</h:panelGroup>
						
						<br /><br />
										
						<p:dataTable value="#{manterAdministradorBean.administradorForm.consultas}" var="consulta" id="table_consultas" emptyMessage="" paginator="true" rows="10" 
				                 paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}" 
				                 paginatorPosition="bottom" style="width:100%">
				                 
				                 <f:facet name="header">
				                 	<h:outputText value="Consultas do sistema" />
				                 </f:facet>
				                 
				                 <p:column>
				                 	<f:facet name="header">
				                 		<h:outputText value="Data / Hora Consulta" />
				                 	</f:facet>
				                 	
				                 	<h:outputText value="#{consulta.dataConsulta}">
				                 		<f:convertDateTime pattern="dd/MM/yyyy HH:mm" locale="pt_BR" timeZone="GMT-3" />
				                 	</h:outputText>
				                 </p:column>
				                 
				                 <p:column>
				                 	<f:facet name="header">
				                 		<h:outputText value="Médico" />
				                 	</f:facet>
				                 	
				                 	<h:outputText value="#{consulta.medico.nome}" />
				                 </p:column>
				                 
				                 <p:column>
				                 	<f:facet name="header">
				                 		<h:outputText value="Área médica" />
				                 	</f:facet>
				                 	
				                 	<h:outputText value="#{consulta.medico.areaMedica.areaMedica}" />
				                 </p:column>
				                 
				                 <p:column style="text-align:center">
				                 	<f:facet name="header">
				                 		<h:outputText value="Status" />
				                 	</f:facet>
				                 	
				                 	<h:outputText value="#{manterAdministradorBean.getObterStatus(consulta.flagAtendido)}" />
				                 </p:column>
				                 
				                 <p:column style="text-align:center">
				                 	<f:facet name="header">
				                 		<h:outputText value="Paciente" />
				                 	</f:facet>
				                 	
				                 	<h:outputText value="#{empty consulta.paciente.nome ? '-' : consulta.paciente.nome}" />
				                 </p:column>
				                 
				                 <p:column style="text-align:center">
				                 	<f:facet name="header">
				                 		<h:outputText value="Operações" />
				                 	</f:facet>
				                 	
				                 	<h:panelGroup layout="block">
				                 		<p:commandLink oncomplete="excluir.show();" rendered="#{consulta.paciente == null}">
				                 			<h:graphicImage library="image" name="remove.png" />
				                 			<f:setPropertyActionListener value="#{consulta}" target="#{manterAdministradorBean.administradorForm.consultaSelecionada}" />
				                 		</p:commandLink>
				                 		<h:graphicImage library="image" name="img_remove_disable.png" rendered="#{consulta.paciente != null}" />
				                 		<p:spacer width="5" />
				                 		<p:commandLink oncomplete="editar_consulta.show();" update="pnlConsulta">
				                 			<f:setPropertyActionListener value="#{consulta}" target="#{manterAdministradorBean.administradorForm.consultaSelecionada}" />
				                 			<h:graphicImage id="imgEdit" library="image" name="edit.png" />				                 				
				                 		</p:commandLink>
				                 	</h:panelGroup>
				                 </p:column>
				                 
				        </p:dataTable>
			 		</p:panel>
			 	</h:panelGroup>
				
			</p:panel>
		</h:form>
		
		<ui:include src="modal_excluir_admin.xhtml">
			<ui:param name="bean" value="#{manterAdministradorBean}" />
			<ui:param name="metodo" value="excluirConsulta" />
			<ui:param name="idTabela" value="table_consultas" />
		</ui:include>
		
		<p:dialog header="Editar Consulta" widgetVar="editar_consulta" resizable="false" modal="true" width="500" showEffect="clip" hideEffect="fold">
			<h:form prependId="false">
				<h:panelGrid id="pnlConsulta" columns="4" cellpadding="4" cellspacing="3">
					<h:outputLabel value="Data da consulta:" style="font-weight:bold" />
					<p:inputMask id="data" mask="99/99/9999 99:99" validator="validaDataExistente" value="#{manterAdministradorBean.administradorForm.consultaSelecionada.dataConsulta}" required="true" requiredMessage="Campo obrigatório">
						<f:convertDateTime pattern="dd/MM/yyyy HH:mm" locale="pt_BR" timeZone="GMT-3" />
					</p:inputMask>
					
					
										
					<h:outputLabel value="Médico:" style="font-weight:bold" />
					<h:selectOneMenu id="medicoEdit" value="#{manterAdministradorBean.administradorForm.consultaSelecionada.medico}" required="true" requiredMessage="Campo obrigatório">						
						<f:selectItems value="#{manterAdministradorBean.administradorForm.medicosPorAreaMedicaModalEditar}" var="entity" itemValue="#{entity}" itemLabel="#{entity.nome}" />
						<f:converter converterId="convertEntity"/>
					</h:selectOneMenu>
					
					
					<h:outputText />
					<h:message for="data" id="msgData" style="font-size:10px;color:red"/>
					<h:outputText />
					<h:message for="medicoEdit" id="msgMedico" style="font-size:10px;color:red"/>
					
					<h:outputLabel value="Área da consulta:" style="font-weight:bold" />					
					<p:inputText disabled="true" value="#{manterAdministradorBean.administradorForm.consultaSelecionada.medico.areaMedica.areaMedica}" />
					
					
				</h:panelGrid>
				<br />
				<h:panelGroup layout="block" style="text-align:center">
					<p:commandButton value="Salvar alteração" action="#{manterAdministradorBean.editarConsulta}" update="table_consultas,msgData,msgMedico,panelModal,modal_resposta" oncomplete="ativaModal();" />
					<p:commandButton value="Cancelar" onclick="editar_consulta.hide();" />
				</h:panelGroup>
						
			</h:form>
		</p:dialog>
		
		<p:outputPanel id="panelModal">
			<script type="text/javascript">
				function ativaModal(){
					if(#{!empty requestScope.cad}){
						edicao_consulta.show();
					}
								
				}						
							
			</script>
		</p:outputPanel>
		
		<p:dialog header="Aviso" widgetVar="edicao_consulta" resizable="false" modal="true" width="320" showEffect="clip" hideEffect="fold">
			<h:form prependId="false">
				<h:panelGroup layout="block" style="text-align:center" id="modal_resposta">
					<h:message id="msgConsulta" for="modal_resposta" styleClass="#{requestScope.cad eq 'sucess' ? 'sucesso_consulta' : 'erro_consulta'}" />
					<br /><br />
					<p:commandButton value="#{requestScope.cad eq 'sucess' ? 'OK' : 'Sair'}" style="font-weight:bold;font-size:14px" onclick="edicao_consulta.hide();editar_consulta.hide();" />
				</h:panelGroup>
				
			</h:form>
		</p:dialog>
		
	</ui:define>
</ui:composition>